using System;
using System.Collections.Generic;
using System.Text;
using NBusiness.CodeDom;
using NBusiness.ESharp.Compiler;
using NBusiness.CodeDom.Compiler;

namespace NBusiness.ESharp
{
    [Serializable]
    public class ESharpAuthorization : EntityAuthorization
    {
        protected internal override void Attach()
        {
            //None
        }

        protected override void InitializeAssignmentLogic()
        {
            Assignments.Add(new AssignmentLogic(
                ESharpParser.Expressions.DenyAllow,
                EntityElementType.AuthorizeAllow,
                new AssignmentLogicHandler(AssignAllow),
                EntityElementType.AuthorizeDeclaration));

            Assignments.Add(new AssignmentLogic(
                ESharpParser.Expressions.RoleName,
                EntityElementType.AuthorizeRole,
                new AssignmentLogicHandler(AssignRole),
                EntityElementType.AuthorizeAllow));

            Assignments.Add(new AssignmentLogic(
                ESharpParser.Expressions.AuthorizeType,
                EntityElementType.AuthorizeType,
                new AssignmentLogicHandler(AssignType),
                EntityElementType.AuthorizeRole));

            Assignments.Add(new AssignmentLogic(
                ESharpParser.Expressions.StatementEnd,
                EntityElementType.StatementEnd,
                PostAssignmentAction.Stop,
                EntityElementType.Any));
        }

        private void AssignAllow(object sender, AssignmentLogicEventArgs e)
        {
            switch (e.Elements.Peek().Value)
            {
                case "allow":
                    Allow = true;
                    break;
                case "deny":
                    Allow = false;
                    break;
                default:
                    OnBuildError(new ESharpBuildError(ESharpErrorCode.ES4001, e.Elements.Peek()));
                    break;
            }
        }
        private void AssignRole(object sender, AssignmentLogicEventArgs e)
        {
            Role = e.Elements.Peek().Value.Trim('"');
        }

        private void AssignType(object sender, AssignmentLogicEventArgs e)
        {
            switch (e.Elements.Peek().Value)
            {
                case "insert":
                    Type = EntityAuthorizationType.Insert;
                    break;
                case "fetch":
                    Type = EntityAuthorizationType.Fetch;
                    break;
                case "update":
                    Type = EntityAuthorizationType.Update;
                    break;
                case "delete":
                    Type = EntityAuthorizationType.Delete;
                    break;
                default:
                    OnBuildError(new ESharpBuildError(ESharpErrorCode.ES4003, e.Elements.Peek()));
                    break;

            }
        }
    }
}
